<template>
  <div class="xtx-goods-page" v-if="goodData.categories">
    <!--
      当我们访问某个默认值为null或者undefined的属性的时候会报错
      通过v-if手动控制模板渲染时机, 只有当属性是存在或者有值的时候才正式渲染模板(正式读取属性)
     -->
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goodData.categories[1].id}`">{{
          goodData.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="`/subCategory/${goodData.categories[0].id}`">{{
          goodData.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem>{{ goodData.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
          <GoodImage :image-list="goodData.mainPictures" />
          <!-- 统计数量的组件sales -->
          <GoodSales :good="goodData" />
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <GoodName :good="goodData" />
          <!-- sku组件 -->
          <Sku :goods="goodData" @change="skuChange" />
          <!-- 数据组件 -->
          <XtxNumbox v-model="num" />
          <!-- 按钮组件 -->
          <XtxButton type="primary" style="margin-top: 20px" @click="insertCart"
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodTabs />
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodHot :type="1" />
          <GoodHot :type="2" />
          <GoodHot :type="3" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { findGoods } from '@/api/good'
import { onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import GoodImage from './components/good-image.vue'
import GoodSales from './components/good-sales.vue'
import GoodName from './components/good-name.vue'
import GoodTabs from './components/good-tabs.vue'
import GoodHot from './components/good-hot.vue'
import Sku from './Sku'
import { useStore } from 'vuex'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodImage,
    GoodSales,
    GoodName,
    Sku,
    GoodTabs,
    GoodHot
  },
  setup () {
    // 获取商品信息
    const route = useRoute()
    const goodData = ref({})
    async function loadGoodData () {
      const res = await findGoods(route.params.id)
      goodData.value = res.result
    }
    onMounted(() => {
      loadGoodData()
    })

    const skuData = ref({}) // 抛出的sku数据对象
    function skuChange (sku) {
      console.log(sku)
      if (sku.skuId) {
        skuData.value = sku
      }
    }
    // 商品数量
    const num = ref(1)

    // 加入购物车
    const store = useStore()
    function insertCart () {
      // 1.准备好加入购物车的所有字段数据
      const good = {
        id: goodData.value.id,
        name: goodData.value.name,
        picture: goodData.value.mainPictures[0],
        skuId: skuData.value.skuId,
        price: skuData.value.oldPrice,
        nowPrice: skuData.value.price,
        attrsText: skuData.value.specsText,
        stock: skuData.value.inventory,
        selected: true, // 选中
        isEffective: true, // 有效
        count: num.value // 商品数据
      }
      // 2.触发一个已经准备好的action函数并且把数据当成实参传入即可
      store.dispatch('cart/asyncInsertCart', good)
    }
    return {
      goodData,
      skuChange,
      num,
      insertCart
    }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
</style>
